import {Component, OnInit, ViewChild, TemplateRef} from '@angular/core';
import {PredefineForProjectService, SelectorGen, StatusSelector, BtnsGen, TableGen, RequestInfo} from '@list-page';
import {UserManagmentService} from './user-managment.service';
import {UserService} from '@kylin/user';
import {NzMessageService} from 'ng-zorro-antd';
@Component({
    templateUrl: './user-managment.component.html',
    styleUrls: ['./user-managment.component.less']
})
export class UserManagmentComponent implements OnInit {
    selectorGen: SelectorGen;
    tableGen: TableGen;
    statusSelector: StatusSelector;
    btnsGen: BtnsGen;
    queryReqInfo: RequestInfo;

    curUser: any = {};
    isVisibleDetails = false;
    isVisiblePassword = false;
    selectedUser: any;
    funSet: any = {};
    @ViewChild('operationTpl') operationTpl: TemplateRef<void>;

    constructor(private userSvc: UserService, private userManaservice: UserManagmentService,  private message: NzMessageService) {
    }

    ngOnInit() {
        this.curUser = this.userSvc.get();
        this.selectorGen = {
            selectorArr: [
                {
                    label: '用户名',
                    key: 'username',
                    placeholder: '请输入用户名'
                },
                {
                    label: '昵称',
                    key: 'nickname',
                    placeholder: '请输入用户昵称'
                },
                {
                    label: '状态',
                    key: 'enabled',
                    type: 'dropdown',
                    dataSource: [
                        {label: '启用', value: true},
                        {label: '禁用', value: false}
                    ]
                },
                {
                    label: '用户类型',
                    key: 'type',
                    type: 'dropdown',
                    dataSource: [
                        {label: '草地', value: 1},
                        {label: '耕地', value: 2},
                        {label: '林地', value: 3},
                        {label: '其他', value: 4},
                    ]
                }
            ],
            splitScale: 3,
            isCollapse: false,
            btnPosition: 'topRight',
            dataProcess: (selectedMap) => {
                return selectedMap;
            }
        } // 筛选条件
        this.btnsGen = {
            right: [
                {
                    name: '添加',
                    zorroType: 'default',
                    klClick: this.addUser
                }
            ]
        } // 按钮管理
        this.tableGen = {
            cols: [
                {head: '用户名', prop: 'username'},
                {head: '性别', prop: 'sex'},
                {head: '用户昵称', prop: 'nickname'},
                {head: '状态', prop: 'enabled'},
                {head: '手机号', prop: 'phone'},
                {head: '用户角色', prop: 'role'},
                {head: '用户类型', prop: 'type'},
                {head: '操作', prop: 'operation', type: 'template'}
            ],
            serialNumber: true,
            noChecked: true,
            dataProcess: (data, funset) => {
                data.map(item => {
                    item.sex = item.sex === 1 ? '男' : '女';
                    item.enabled = item.enabled === true ? '启用' : '禁用';
                    item.type = item.type === '1' ? '草地' : (item.type === '2' ? '耕地' : (item.type === '3' ? '林地' : '其他'));
                    item.role = item.role === 'USER' ? '普通用户' : (item.role === 'GRASSLAND_ADMIN' ? '草地管理员' : (item.role === 'FOREST_ADMIN' ?  '林地管理员' : (item.role === 'SUPER_ADMIN' ? '超级管理员' : '耕地管理员') ));
                    item.operation = this.operationTpl;
                });
                return data;
            }
        } // 列表管理
        this.queryReqInfo = {
            path: '/users',
            params: {}, // 静态参数，不会改变的
            method: 'GET'
        }; // 列表请求设置
    }

    // 添加用户
    addUser = (data, funSet) => {
        this.isVisibleDetails = true;
        this.setSelectedUser(void 0);
        this.funSet = funSet;
    }

    // 修改用户信息
    modifyUser = (data, funSet) => {
        this.setSelectedUser(data);
        this.isVisibleDetails = true;
        this.funSet = funSet;
    }

    // 删除用户
    deleteUser(data, funSet) {
        this.userManaservice.deleteUser(data.id || '').then(rep => {
            if (rep.resp_code === '10000') {
                this.message.success(`删除用户 ${data.username} 成功`);
                funSet.refreshList();
            } else {
                this.message.error(`删除用户 ${data.username} 失败，${rep.resp_msg}`);
            }
        });
    }

    // 重置密码
    resetPassword(data, funSet) {
        this.isVisiblePassword = true;
        this.setSelectedUser(data);
    }

    setSelectedUser(userInfo) {
        this.selectedUser = userInfo;
    }

    handleProcessRst(rst) {
        if (rst) {
            this.funSet.refreshList();
        }
    }

}
